<?php
class Merchandise
{               
    // Public variables to be used in Smarty template
    public $mMerchandise;
    public $mMerchandiseLocations;
    public $mLinkToContinueShopping;
    public $mLocations;
    public $mTracklist;
    
    // Private stuff
    private $_mMerchandiseId;
    
    // Class constructor
    public function __construct()      
    {
        // Variable initialization
        if (isset($_GET['MerchandiseId']))
            $this->_mMerchandiseId = (int)$_GET['MerchandiseId'];
        else
            trigger_error('Product ID not set');
        
        // Save page request for continue shopping functionality
        $_SESSION['link_to_continue_shopping'] = $_SERVER['QUERY_STRING'];
    }
    
    public function init()
    {
        // Get product details from business tier
        $this->mMerchandise = Catalog::GetMerchandiseDetails($this->_mMerchandiseId);
        if(isset($_SESSION['link_to_continue_shopping']))
        {   
            $continue_shopping = Link::QueryStringToArray($_SESSION['link_to_continue_shopping']);
            
            $page = 1;
            
            if(isset($continue_shopping['Page']))
                $page = (int)$continue_shopping['Page'];
                
            if(isset($continue_shopping['CategoryId']))
                $this->mLinkToContinueShopping = Link::ToCategory($continue_shopping['CategoryId'], $page);
                
            if(isset($continue_shopping['GenreId']))
                $this->mLinkToContinueShopping = Link::ToCategory($continue_shopping['GenreId'], $page);
        }
        
        if($this->mMerchandise['picture'])
            $this->mMerchandise['link_to_picture'] = Link::Build('product_images/'.$this->mMerchandise['picture']);
        
        if($this->mMerchandise['category_id'] == 1)
            $this->mTracklist = Catalog::GetMerchandiseTracklist($this->_mMerchandiseId);
        
        $this->mLocations = Catalog::GetProductLocations($this->_mMerchandiseId);
        
        // Build links for product departments and categories pages
        for($i = 0;$i < count($this->mLocations);$i++)
        {
            $this->mLocations[$i]['link_to_genre'] = Link::ToGenre($this->mLocations[$i]['genre_id']);
            $this->mLocations[$i]['link_to_category'] = Link::ToCategroy($this->mLocations[$i]['category_id']);
        }
    }
}               
?>
